package com.yike.product.controller;

import com.yike.common.api.ApiResult;
import com.yike.common.api.Update;
import com.yike.common.base.BaseController;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.pagination.Paging;
import com.yike.product.param.PlatformGoodsPageParam;
import com.yike.product.param.PlatformGoodsParam;
import com.yike.product.param.PlatformShopGoodsPageParam;
import com.yike.product.service.PlatformGoodsService;
import com.yike.product.vo.PlatformGoodsVo;
import com.yike.product.vo.PlatformShopGoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品库管理 控制器
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@RestController
@RequestMapping("/platform/goods")
@Module("product")
@Api(value = "商品库管理API", tags = {"平台商品库管理"})
public class PlatformGoodsController extends BaseController {

    @Autowired
    private PlatformGoodsService platformGoodsService;

    /**
     * 批量添加商品
     */
    @PostMapping("/batchSave")
    @OperationLog(name = "批量添加商品", type = OperationLogType.ADD)
    @ApiOperation(value = "批量添加商品", response = ApiResult.class)
    public ApiResult<Boolean> batchSave(@Validated @RequestBody List<PlatformGoodsParam> platformGoodsParams) throws Exception {
        boolean flag = platformGoodsService.batchSave(platformGoodsParams);
        return ApiResult.result(flag);
    }

    /**
     * 批量添加商品
     */
    @PostMapping("/batchUpdate")
    @OperationLog(name = "批量修改商品", type = OperationLogType.UPDATE)
    @ApiOperation(value = "批量修改商品", response = ApiResult.class)
    public ApiResult<Boolean> batchUpdate(@Validated @RequestBody List<PlatformGoodsParam> platformGoodsParams) throws Exception {
        boolean flag = platformGoodsService.batchUpdate(platformGoodsParams);
        return ApiResult.result(flag);
    }

    /**
     * 批量添加商品
     */
    @PostMapping("/batchDel")
    @OperationLog(name = "批量删除商品", type = OperationLogType.UPDATE)
    @ApiOperation(value = "批量删除商品", response = ApiResult.class)
    public ApiResult<Boolean> batchDel(@Validated(Update.class) @RequestBody List<Long> ids) throws Exception {
        boolean flag = platformGoodsService.batchDel(ids);
        return ApiResult.result(flag);
    }

    @GetMapping("/info/{id}")
    //@OperationLog(name = "商品详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品详情", response = PlatformGoodsVo.class)
    public ApiResult<PlatformGoodsVo> getGoods(@PathVariable("id") Long id) throws Exception {
        PlatformGoodsVo platformGoodsVo = platformGoodsService.getGoods(id);
        return ApiResult.ok(platformGoodsVo);
    }

    @PostMapping("/getGoodsList")
    //@OperationLog(name = "商品库列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品库列表", response = PlatformGoodsVo.class)
    public ApiResult<Paging<PlatformGoodsVo>> getGoodsList(@Validated @RequestBody PlatformGoodsPageParam platformGoodsPageParam) throws Exception {
        Paging<PlatformGoodsVo> paging = platformGoodsService.getGoodsList(platformGoodsPageParam);
        return ApiResult.ok(paging);
    }

    @PostMapping("importGoods")
    @ApiOperation(value = "导入商品", response = ApiResult.class)
    public ApiResult<Boolean> importGoods(@RequestParam(value = "file") MultipartFile file) throws Exception {
        boolean flag = platformGoodsService.importGoods(file);
        return ApiResult.result(flag);
    }

    @GetMapping("exportTemplate")
    @ApiOperation(value = "导出模板")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        platformGoodsService.exportTemplate(request, response);
    }

    @PostMapping("/getShopGoodsList")
    //@OperationLog(name = "店铺商品列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "店铺商品列表", response = PlatformShopGoodsVo.class)
    public ApiResult<Paging<PlatformShopGoodsVo>> getShopGoodsList(@Validated @RequestBody PlatformShopGoodsPageParam platformShopGoodsPageParam) throws Exception {
        Paging<PlatformShopGoodsVo> paging = platformGoodsService.getShopGoodsList(platformShopGoodsPageParam);
        return ApiResult.ok(paging);
    }

}

